
using Identity.Application.Contracts.Common;
using Identity.Application.Permissions.Interfaces;
using Identity.Domain.Aggregates;
using Identity.Infrastructure.Configurations;
using Microsoft.EntityFrameworkCore;

namespace Identity.Infrastructure.Persistence.Repositories;

public class PermissionRepository : IPermissionsRepository
{
    private readonly ApplicationDbContext _context;
    public PermissionRepository(ApplicationDbContext context)
    {
        _context = context;
    }
    public async Task AddAsync(Permission permission)
    {

        await _context.Permissions.AddAsync(permission);
    }

    public Task DeleteAsync(Permission permission)
    {
        _context.Permissions.Remove(permission);
        return Task.CompletedTask;
    }

    public async Task<Permission?> GetPermissionByIdAsync(Guid Id)
    {
        
        return await _context.Permissions.FindAsync(Id);
    }

    public async Task<bool> IsBindingApi(Guid Id)
    {
        var apiperIds = _context.ApiPermissionBindings.Select(aper => aper.permissionId).ToHashSet();

        return  apiperIds.Contains(Id);
    }
}